package jinyilw.swing.visuals.window;

import jinyilw.swing.ColorTools;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.LayoutManager;
import java.awt.Window;

public class TransWindow extends Window
{
	private static final long serialVersionUID = 6596272443433783136L;
	private final ContentContainer contentContainer = new ContentContainer();

	public TransWindow(Window owner)
	{
		super(owner);
		init();
	}

	private void init()
	{
		super.addImpl(contentContainer, null, -1);
		setBackground(ColorTools.transparentColor);
	}

	@Override
	public void setBounds(int x, int y, int width, int height)
	{
		boolean unResized = width == getWidth() && height == getHeight();
		super.setBounds(x, y, width, height);
		if (unResized || contentContainer == null)
			return;
		contentContainer.setBounds(0, 0, width, height);
	}

	@Override
	public void setLayout(LayoutManager mgr)
	{
		if (contentContainer != null)
			contentContainer.setLayout(mgr);
	}

	@Override
	protected void addImpl(Component comp, Object constraints, int index)
	{
		if (contentContainer != null)
			contentContainer.addImpl(comp, constraints, index);
	}

	@Override
	public Dimension getPreferredSize()
	{
		if (contentContainer != null)
			return contentContainer.getPreferredSize();
		return super.getPreferredSize();
	}

	protected void paintBackground(Graphics g, int width, int height)
	{

	}

	class ContentContainer extends Container
	{
		private static final long serialVersionUID = -4787341697854949549L;

		@Override
		public void paint(Graphics g)
		{
			paintBackground(g, getWidth(), getHeight());
			super.paint(g);
		}

		@Override
		protected void addImpl(Component comp, Object constraints, int index)
		{
			super.addImpl(comp, constraints, index);
		}
	}
}
